﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using ERP.Domain.Workorders;
using MediatR;
using Sell.Api.Read.Application.Command.GoodCommand;
using Sell.DTO;
using SqlSugar;

namespace Sell.Api.Read.Application.CommandHandlers.GoodCommandHandlers
{
    public class GoodlistCommandHandlers : IRequestHandler<GoodlistCommand, ApiResult<ApiPaging<gooddto>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public GoodlistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 货品信息列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<ApiPaging<gooddto>>> Handle(GoodlistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<gooddto>> res = new ApiResult<ApiPaging<gooddto>>();
            res.Code = EnumCode.Ok;
            res.Message = "采购单显示成功";
            /// 初始化查询：关联 Procure表和Contract表
            var query = SqlSugar.Queryable<Good>()
                .LeftJoin<Supplier>((g, s) => g.SupplierId == s.Id)
                .LeftJoin<Customer>((g, s, c) => g.CustomerId == c.Id)
                .Where(g => g.IsDelete == false).Select(
                (g, s, c) => new gooddto
                {
                    GoodName=g.GoodName,
                     CustomerType=c.CustomerType,
                      CustomerName=c.CustomerName,
                       CustomerId=g.CustomerId,
                        CustomerCode=c.CustomerCode,
                         Cprice=g.Cprice,
                          Company=g.Company,
                           Desc= c.Desc,
                            GoodCode=g.GoodCode,
                             GoodState=g.GoodState,
                              Id= g.Id,
                               People=s.People,
                                Phone=s.Phone,
                                 Rprice= g.Rprice,
                                  Specification=g.Specification,
                                   State=c.State,
                                    SupplierCode=s.SupplierCode,
                                     SupplierId=g.SupplierId,
                                      SupplierLevel=s.SupplierLevel,
                                       SupplierName=s.SupplierName,
                                        UserPeople=c.UserPeople,
                });
            if (!string.IsNullOrEmpty(request.GoodName))
            {
                query = query.Where(g => g.GoodName.Contains(request.GoodName));
            }
            if (request.GoodState != null)
            {
                query = query.Where(g => g.GoodState.Equals(request.GoodState));
            }
            //SqlSugar分页查询实现
            ApiPaging<gooddto> result = new ApiPaging<gooddto>();
            result.TotalCount = query.Count();//查询总记录数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);//计算总页数
            result.Data = query.Skip((request.pageindex - 1) * request.pagesize)//跳过前面的记录
                .Take(request.pagesize)//取指定页大小的记录
                .ToList();//执行查询并转为List
            res.Success(result, "查询成功");
            return res;
        }
    }
}
